package com.example.administrator.printoutindemo;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.dds.soap.SoapListener;
import com.dds.soap.SoapParams;
import com.dds.soap.SoapUtil;
import com.example.administrator.printoutindemo.base.BaseActivity;
import com.example.administrator.printoutindemo.services.Tools;
import com.example.administrator.printoutindemo.services.dyjApplication;
import com.jess.arms.base.BaseConstants;
import com.xiaoxing.gifloadingview.LoadingDialogUtil;

import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;

import java.util.ArrayList;
import java.util.List;

public class StoreListMonthkedZLJY extends BaseActivity {

    private static final String WEBSERVICE_CONFIG = "webServiceInfo";
    private static final int LOGINSUCCESS = 0;
    private static final int NETWORKERROR = 2;
    private static final int SYSERROR = 3;
    private ListView store_listView_monthsked = null;
    private Mothsked mothsked = null;
    private List<Mothsked> li_mo = null;
    private StoreListMonthkedAdapter adapter = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.store_activity_listmonthked);

        store_listView_monthsked = (ListView) findViewById(R.id.store_listView_monthsked);

        mothsked = new Mothsked();
        adapter = new StoreListMonthkedAdapter(this);

        final int user_id = ((dyjApplication) getApplication()).get_user_id();

        if (user_id != 0) {
            getVmonthskedByZLJY(String.valueOf(user_id));

        } else {
            Tools.showDialogInfo(this, "登录已过期，请重新登录");
        }

    }

    private void getVmonthskedByZLJY(String user_id) {


        String methodName = "GetVmonthskedByZLJY";
        SoapParams params = new SoapParams();
        params.put("user_id", user_id);
        /**
         * 请求
         */
        SoapUtil.getInstance(this).callEntry(getUrl(), BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                LoadingDialogUtil.dismissDialog();

                String result = object.getProperty(methodName + "Result").toString();
                if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {
                    if (result.equals("true")) {
                        li_mo = mothsked.getListFromJSON(result);
                        bindList();
                    }
                } else {

                }

            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                LoadingDialogUtil.dismissDialog();
            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                LoadingDialogUtil.dismissDialog();
            }
        }, getAapiUserName(), getApiPwd());
    }


    private void bindList() {

        List<Mothsked> li = new ArrayList<Mothsked>();

        for (Mothsked m : li_mo) {
            if (m.getMstatus() == 4) {
                li.add(m);
            }
        }

        adapter.setList(li);
        store_listView_monthsked.setAdapter(adapter);
        // 点击
        store_listView_monthsked
                .setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {

                        view.setBackgroundResource(R.color.danqing);

                        Intent intent = new Intent(StoreListMonthkedZLJY.this,
                                StoreQRCScanByZLJY.class);

                        Bundle mBundle = new Bundle();

                        mBundle.putSerializable("Mothsked",
                                li_mo.get(position));
                        intent.putExtras(mBundle);

                        startActivity(intent);
                    }
                });

    }


}